package utils.message;

import org.codehaus.jettison.json.JSONObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import org.codehaus.jettison.json.JSONException;
import regression.User;

public class Message {

    public static final int MAX = 1000000;
    JSONObject obj;
    String id;
    User reciever;

    public Message(MessageContents messageType) throws JSONException {
        obj = new JSONObject();
        setId(generateID());
        put(MessageTags.TYPE.getTag(), messageType.getContent());
        put(MessageTags.ID.getTag(), getId());
    }

    public String generateID() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss-");
        return dateFormat.format(new Date()) + (new Random()).nextInt() % MAX;
    }

    public JSONObject getObj() {
        return obj;
    }

    public void setObj(JSONObject obj) {
        this.obj = obj;
    }

    public User getReciever() {
        return reciever;
    }

    public void setReciever(User reciever) {
        this.reciever = reciever;
    }

    @SuppressWarnings("unchecked")
    public void put(String tag, String content) throws JSONException {
        obj.put(tag, content);
    }

    public void put(MessageTags tag, String content) throws JSONException {
        obj.put(tag.getTag(), content);
    }

    public String toString() {
        return obj.toString();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}